package com.bjgov.scoresys.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.bjgov.scoresys.entity.ScoreItem;
import com.bjgov.scoresys.entity.ScoreTree;
import com.bjgov.scoresys.form.ScoreSubItemsForm;
import com.bjgov.scoresys.service.ScoreSubItemsHandler;

public class ScoreSubItemsAction  extends MappingDispatchAction{
	
	private static Log log = LogFactory.getLog(ScoreSubItemsAction.class);
	
	/**
	 * 添加评分子项，需要判断pid是不是含有最终子项
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addScoreItems(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception
	{
		
		ScoreSubItemsForm itemsForm = (ScoreSubItemsForm)form;

		log.info("-------------items-content-------" + itemsForm.getContent());
		
		if(itemsForm.getItemname().length()==0)
		{
			request.setAttribute("message", "评分子项名称不能为空！");
			request.setAttribute("returnURL", "/system/addScoreSubItems.jsp");
			return mapping.findForward("fail");
		}
		
		ScoreTree scoreTree = itemsForm.getScoreTree();
		if(scoreTree.getHasScoreItem()==false)
		{
			request.setAttribute("message", scoreTree.getCateName()+"不包含最终评分子项！");
			request.setAttribute("returnURL", "/system/addScoreSubItems.jsp");
			return mapping.findForward("fail");
		}
		
		//加入区别质量行为和质量实体字段 id 1,40,44-质量行为(Quality_Type=1)  3-质量实体(Quality_Type=2)
		//根据ScoreTree实体deep_path来判断
//		short qualityType=1;
//		String[] deep_path = scoreTree.getDeepPath().split(",");
//		if(Short.parseShort(deep_path[0])==3)
//		{
//			qualityType=2;
//		}
		ScoreItem item = new ScoreItem();
		item.setItemName(itemsForm.getItemname());
		item.setItemDesc(itemsForm.getItemdesc());
		item.setScoreTree(scoreTree);
		item.setItemUrlDesc(itemsForm.getContent());
		item.setStatus((short)0);
		//item.setQualityType(qualityType);
		
		ScoreSubItemsHandler.save(item);
		
		
		request.setAttribute("message", "添加成功！");
		request.setAttribute("nextURL", mapping.getInput());
		return mapping.findForward("tip");
	}
	
}
